package thread;

import org.w3c.dom.ls.LSOutput;

public class SyncDemo {
    private static boolean success1;
    private static boolean success2;
    public static void main(String[] args) {

        int coount = 1;
        while (true) {
            Bank bank = new Bank();
            Thread t1 = new Thread(()->success1 = bank.getMoney(20000));
            Thread t2 = new Thread(()->success2 = bank.getMoney(20000));
            t1.start();
            t2.start();
            try{
                Thread.sleep(20);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            if(success1 && success2){
                System.out.println("卡bug成功同时取到钱");
                System.out.println("经历"+coount+"次");
                break;
            }else{
                System.out.println("正常，一人取到钱");
                coount++;
            }
            if(coount > 100){
                break;
            }
        }

    }
}

class Bank{
    private int account = 20000;
    public synchronized boolean getMoney(int money){
        int account = getAccount() ;
        if( account >= money){
            account = account - money;
            Thread.yield();
            saveAccount(account);
            return true;
        }else{
            return false;
        }
    }

    public int getAccount(){
        return account;
    }

    public void saveAccount(int money) {
        account =  money;
    }
}














